<?php
App::uses('AppController', 'Controller');
/**
 * Categories Controller
 *
 * @property Category $Category
 */
class CategoriesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Category->recursive = 0;
		$this->set('categories', $this->paginate());
                
                //$this->set('items', $this->Item->find('all'));

        $this->pageTitle = '- Categories';
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Category->exists($id)) {
			throw new NotFoundException(__('Invalid category'));
		}
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
		
		$this->set('category', $this->Category->find('first', $options));
		$this -> set('categories', $this -> Category -> find('all'));
                
                //$this->set('items', $this->Category->Item->find('all', array('conditions' => array('category_id' => $id))));

        $this->pageTitle = '- View a Category';
	}

/**
 * add method
 *
 * @return void

    public function add() {
        if ($this->request->is('post')) {
            $this->Category->create();
            if ($this->Category->save($this->request->data)) {
                $this->Session->setFlash(__('The category has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
            }
        }
    }
 */

    public function add() {

        $this->pageTitle = '- Add a Category';

        if ($this->request->is ( 'post' )) {
            $this->Category->create ();
            // save data
            $result = $this->Category->save ( $this->request->data );

            $msg = array (
                __ ( 'The category has been saved' ),
                __ ( 'The category could not be saved. Please, try again.' )
            );
            // if ajax request, then back to json data
            if ($this->request->is ( "ajax" )) {
                header ( "Pragma: no-cache" );
                header ( "Cache-Control: no-store, no-cache, max-age=0, must-revalidate" );
                header ( 'Content-Type: text/x-json' );
                header ( 'Content-type: application/json' );
                $categorylist = array ();
                $status=false;
                if ($result) {
                    $category_all = $this->Category->find ( "all" );

                    if (! empty ( $category_all )) {
                        foreach ( $category_all as $v ) {
                            $categorylist[]=$v["Category"];
                        }
                    }
                    $status=true;
                }

                $data = array (
                    "status" => $status,
                    "message" => $status ? $msg [0] : $msg [1],
                    "categorylist" => $categorylist
                );

                echo json_encode ( $data );
                die ();
            } elseif ($result) {
                $this->Session->setFlash ( $msg [0] );
                $this->redirect ( array (
                    'action' => 'index'
                ) );
            } else {
                $this->Session->setFlash ( $msg [1] );
            }
        }
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Category->exists($id)) {
			throw new NotFoundException(__('Invalid category'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('The category has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
			$this->request->data = $this->Category->find('first', $options);
		}

        $this->pageTitle = '- Edit a Category';

    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			throw new NotFoundException(__('Invalid category'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Category->delete()) {
			$this->Session->setFlash(__('Category deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Category was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
        
        //----------------------------------------------------------------------
        
        public function beforeFilter() {
            parent::beforeFilter();
            
            $this->Auth->autoRedirect = false;
            $this->Auth->allow('view');
            $this->Auth->deny('*');   
        }

    public function search() {
        //$this->set('results',$this->Customer->search($this->data['Customer']['q']));

        $keyword=$this->request->data['Category']['keyword']; //get keyword from querystring//
        //used simpme or condition with singe value checking
        //replace ModelName with actual name of your Appmodel

        //$this->Session->setFlash($keyword);

        $conditions=array('OR'=>array(
            'Category.id LIKE'=> $keyword,
            'Category.name LIKE'=> $keyword,
            'Category.description LIKE'=> $keyword));

        $this->set('isFound', $this->Category->find('count',array(
                    'conditions'=>$conditions)
            )
        );

        $this->set('list', $this->Category->find('all',array(
                    'conditions'=>$conditions)
            )
        );
    }

}
